/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ejb;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import jpa.CredenzialiTutor;
import jpa.Tutor;

/**
 *
 * @author admin
 */
@Stateless
public class GestoreTutor implements GestoreTutorLocal {

    @PersistenceContext
    private EntityManager em;

    @Override
    public Tutor login(String user, String pass) {
         CredenzialiTutor ct = em.find(CredenzialiTutor.class, user);
        if(ct != null){
           String hashword = null;
           try {
               MessageDigest md5 = MessageDigest.getInstance("MD5");
               md5.update(pass.getBytes());
               BigInteger hash = new BigInteger(1, md5.digest());
               hashword = hash.toString(16);
           }catch (NoSuchAlgorithmException nsae) {
                // ignore
           }
           if(ct.getPassword().equals(hashword)){
               Tutor t= em.find(Tutor.class, ct.getTutorid());
               return t;
           }
        }
        return null;
    }

    @Override
    public boolean registra(CredenzialiTutor ct, Tutor r) {
        CredenzialiTutor c = em.find(CredenzialiTutor.class, ct.getUsername());
        if(c != null) {
            return false;
        }
        em.persist(r);
        em.flush();
        ct.setTutorid(r.getId());
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(ct.getPassword().getBytes());
            BigInteger hash = new BigInteger(1, md5.digest());
            ct.setPassword(hash.toString(16));
        }catch (NoSuchAlgorithmException nsae) {
                // ignore
        }
        em.persist(ct);
        return true;
    }

    @Override
    public List<Tutor> listaTutor() {
        Query query = em.createNamedQuery("Tutor.findAll");
        return query.getResultList();
    }

    @Override
    public boolean assegna(Integer idTutor, Integer idCorso) {
        Tutor t = em.find(Tutor.class, idTutor);
        if(t!=null){
            t.setCorsiid(idCorso);
            em.merge(t);
            em.flush();
            return true;
        }
        return false;
    }

    @Override
    public CredenzialiTutor controllaUsername (CredenzialiTutor ct){
        return em.find(CredenzialiTutor.class, ct.getUsername());
    }

    @Override
    public boolean modifica(Tutor t) {
        Tutor c = getTutor(t.getId());
        if(c != null){
            em.merge(t);
            em.flush();
            return true;
        }
        return false;
    }

    @Override
    public Tutor getTutor(Integer id) {
        return em.find(Tutor.class, id);
    }

    @Override
    public boolean elimina(Integer Id) {
        Tutor t = getTutor(Id);
        CredenzialiTutor ct = getCredenzialiTutor(Id);
        if(t==null||ct==null) {
            return false;
        }
        em.remove(t);
        em.flush();
        return true;
    }

    @Override
    public CredenzialiTutor getCredenzialiTutor(Integer tutorId) {
        Query query = em.createNamedQuery("CredenzialiTutor.findByTutorid");
        query.setParameter("tutorid", tutorId);
        CredenzialiTutor t = (CredenzialiTutor) query.getResultList().get(0);
        return t;
    }

    @Override
    public String getNomeTutor(Integer tutorid) {
        Tutor t = em.find(Tutor.class, tutorid);
        return t.getCognome()+" "+t.getNome()+" - Matricola "+t.getId();
    }
 
}
